package com.zhq.blog.manage.domain.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhq.blog.core.domain.admin.Operator;

/**
 * 后台登陆
 * @title:LoginController.java
 * @package com.zhq.blog.manage.domain.controller
 * @author 张恒强
 * @date 2014年12月30日 下午3:18:12
 * @version V1.0
 */
@Controller
@RequestMapping("/admin/login/*")
public class AdminLoginController extends BaseController {
	public static final String OPERATOR_FLAG = "operator";
	public static final String OPERATOR_PRIZE = "prize";
	public static final String OPERATOR_USER = "user";
	public static final String OPERATOR_GAME = "game";
	public static final String OPERATOR_SAFE = "safe";
	public static final String OPERATOR_MONEY = "money";
	@RequestMapping(value = {"index","/",""})
	public BossModel index(){
		return createModel("/admin/login");
	}
	
	@RequestMapping(value ="valid",method = RequestMethod.POST)
	public @ResponseBody String valid(HttpServletRequest request,String username,String password) throws UnknownHostException{
		List<Operator> list = context.getOperatorRepository().findByLoginNameAndPassword(username, password);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		InetAddress addr = InetAddress.getLocalHost();
		if(list.size() != 0){
			Operator operator = new Operator();
			operator.setId(1L);
			operator.setName(list.get(0).getName());
			list.get(0).setIp(addr.getHostAddress().toString());
			list.get(0).setLoginTime(sdf.format(new Date()));
			context.getOperatorRepository().save(list.get(0));
			this.getSession().setOperator(request,list.get(0));
			return "ture";
		}else{
			return "false";
		}
		
	}
	
}
